<?php

namespace app\admin\controller\sms;

use app\common\controller\Backend;
use think\Db;
/**
 * 用户数据管理
 *
 * @icon fa fa-circle-o
 */
class Customer extends Backend
{

    /**
     * Customer模型对象
     * @var \app\admin\model\sms\Customer
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sms\Customer;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 导入
     */
    public function import(){ 
    	
    	$file = $this->request->request('file'); //'file'为文件字段名
    	$data = $this->readFile($file); 
    	$array = ['姓名' => 'name','手机号' => 'mobile','生日' => 'birth_date',
    			'护照号' => 'passport_no','护照到期日' => 'passport_expiration_date',
    			'添加日期' => 'add_date','备注' => 'remark'
    	];
    	//
    	$update_num = $insert_num = 0;
    	foreach ($data as  $row){
    		//do something
    		$temp = [];
    		foreach ($row as $key => $val){
    			$temp[$array[$key]] = $val;
    		} 
    		if(!empty($temp)){
    			$customer = Db::name('sms_customer')->where('mobile', $temp['mobile'])->find();
    			if($customer){
    				Db::name('sms_customer')->where('mobile', $temp['mobile'])->update($temp);
    				$update_num ++;
    			}else{
    				Db::name('sms_customer')->insert($temp);
    				$insert_num ++;
    			}
    		} 
    	}
    	
    	$this->success('导入成功,新增['.$insert_num.']条数据，更新['.$update_num.']条数据.'); 
    
    }

}
